package com.ldk.base.framework.datasource.dispatcher;

import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ldk.base.framework.dao.IDbconfigDAO;
import com.ldk.base.framework.datasource.CustomNoXADataSourceFactory;
import com.ldk.base.framework.datasource.CustomSqlSessionManager;
import com.ldk.base.framework.datasource.CustomXADataSourceFactory;
import com.ldk.base.framework.datasource.DataSourceDriver;
import com.ldk.base.framework.entity.Dbconfig;

/**
 * 数据源调度器
 * @author xiangwl
 */
@Component
public class DataSourceDispatcher {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private IDbconfigDAO dbconfigDAO;
	
	/**
	 * 检查器一分钟检查一次
	 */
	@Scheduled(cron="0 0/1 * * * ?")
	public void detector(){
		try{
			Dbconfig query = new Dbconfig();
			query.setEnabled(true);
			//默认数据库连接
			List<Dbconfig> dbConfigs = dbconfigDAO.selectList("", query);
			if(dbConfigs != null && !dbConfigs.isEmpty()){
				for(Dbconfig item : dbConfigs){
					if(StringUtils.isEmpty(item.getDbType()) ||
					   StringUtils.isEmpty(item.getServerName())  ||
					   StringUtils.isEmpty(item.getDatabaseName())  ||
					   item.getPortNumber() < 1  ||
					   StringUtils.isEmpty(item.getDbUser()) ||
					   StringUtils.isEmpty(item.getDbPassword()))
					{
						continue;
					}
					if(CustomSqlSessionManager.getInstance().existDataSource(item.getDbKey()))
						continue;
					
					DataSourceDriver driver = null;
					if(item.getDbType().equalsIgnoreCase("mysql")){
						driver = DataSourceDriver.MYSQL;
					}
					else if(item.getDbType().equalsIgnoreCase("mssql")){
						driver = DataSourceDriver.MSSQL;
					}
					else if(item.getDbType().equalsIgnoreCase("oracle")){
						driver = DataSourceDriver.ORACLE;
					}
					else if(item.getDbType().equalsIgnoreCase("postresql")){
						driver = DataSourceDriver.POSTRESQL;
					}
					else if(item.getDbType().equalsIgnoreCase("db2")){
						driver = DataSourceDriver.DB2;
					}
					if(driver == null) continue;
					
					DataSource dataSource = null;
					if(item.getIsxa()){
						dataSource = CustomXADataSourceFactory.createDataSource(driver, 
							    item.getDbKey(),
								item.getServerName(),
								item.getPortNumber(),
								item.getDatabaseName(),
								item.getDbUser(), 
								item.getDbPassword());
					}
					else{
						dataSource = CustomNoXADataSourceFactory.createDataSource(driver, 
								item.getServerName(),
								item.getPortNumber(),
								item.getDatabaseName(),
								item.getDbUser(), 
								item.getDbPassword());
					}
					if(dataSource == null) continue;
						
					CustomSqlSessionManager.getInstance().addDataSource(item.getDbKey(), dataSource);
				}
			}
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
	}
	
}
